#include "../mars/latch.cc"
#include "../mars/thread.cc"
#include "../mars/logger.h"
#include <boost/bind.hpp>

using namespace mars;


/*
 * @brief Latch 意为门闩，用于帮助解决只有某个条件成立才进行下一步
 */

Latch latch;

void task(const char* task_name) {
    // 这里做一些和初始化无关事情
    INFO_PRINT("Hello, I'm %s and do somethings\n", task_name);

    // 下面这些事情，必须要等到初始化完成才能做
    latch.wait();
    INFO_PRINT("Hello, I'm %s. I know that initialization has finished. I'm going to next step...\n", task_name);
}


void init() {
    // 一些耗时的操作
    sleep(5);
}

int main() {
    Thread allen(boost::bind(task, "allen"));
    Thread luffy(boost::bind(task, "luffy"));
    Thread zoro(boost::bind(task, "zoro"));
    Thread sanji(boost::bind(task, "sanji"));

    allen.start();
    luffy.start();
    zoro.start();
    sanji.start();

    INFO_PRINT("Hello, I'm main. I'm ready to initialize\n");
    init();
    latch.open();


    allen.join();
    luffy.join();
    zoro.join();
    sanji.join();

    return 0;
}
